package cn.itsource.order.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.LoginContext;
import cn.itsource.basic.util.PageList;
import cn.itsource.order.domain.OrderPetAcquisition;
import cn.itsource.order.dto.OrderDto;
import cn.itsource.order.mapper.OrderPetAcquisitionMapper;
import cn.itsource.order.query.OrderPetAcquisitionQuery;
import cn.itsource.order.service.IOrderPetAcquisitionService;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;


    @Override
    public PageList<OrderPetAcquisition> orderQueryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginUser(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        // 判断登录员工是那种角色
        //1.employee中shop_id为null，平台管理员
        Long shopId = employee.getShopId();
        //2.店铺人员
        if (shopId!=null){
            Shop shop = shopMapper.loadById(shopId);
            if(!shop.getAdminId().equals(employee.getId())){//店员 //注意：Long包装类不能用==判断，long才行
                //3.否则就为店员
                query.setEmpId(employee.getId());
            }
            //4.employee中shop_id不为null，通过shop_id查询店铺得到admin_id如果为当前登录管理员的id，就为店铺管理员
            query.setShopId(shopId);
        }
        //5.平台管理员
        return super.queryPage(query);
    }

    @Override
    public void confirm(OrderDto orderDto) {
        Long orderId = orderDto.getId();
        BigDecimal money = orderDto.getMoney();
        Integer paytype = orderDto.getPayType();
        //1.修改订单状态：state：0待处理 - 1已处理。价格：要以现在的【收购宠物时】为主，用户寻主消息的价格不是最终的价格 ，支付方式
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderId);
        order.setState(1);
        order.setPrice(money);
        order.setPaytype(paytype);
        orderPetAcquisitionMapper.update(order);

        //2.修改寻主消息状态：state：1待处理 - 2处理完成。价格：可以不改【寻主消息的金额可以与最终价格不一样】
        Long msgId = order.getSearchMasterMsgId();
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(msgId);
        msg.setState(2);
        searchMasterMsgMapper.update(msg);

        //3.信息宠物金额：价格：以线下收购时的价格为主
        Long petId = order.getPetId();
        Pet pet = petMapper.loadById(petId);
        pet.setCostprice(money);
        petMapper.update(pet);
    }

    @Override
    public void remove(Long id) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //1.将宠物放到寻主池：state = 1，shopId置空
        Long msgId = order.getSearchMasterMsgId();
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(msgId);
        msg.setShopId(null);
        searchMasterMsgMapper.update(msg);

        //2.删除宠物信息
        petMapper.remove(order.getPetId());

        super.remove(id);//删除订单
    }
}
